//待办事件列表
appControllers.controller('It_EventListCtrl', function($scope, $http, $state) {
	
	var p = {
		"type" : "DB",
		"flowcode" : "EVENT",
		"dbtype":'1'
	};
	var reqdata = {
		'method' : 'OrderList',
		'param' : JSON.stringify(p)
	};

	$scope.scrollItems = [];
	$scope.hasdata = true;
	
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
	 	for (var i = 0; i < data.object.length; i++) {
				var obj = data.object[i];
				/*if (obj.statu == "0") {
					obj.statuname = "未签收";
				} else if (obj.statu == "1") {
					obj.statuname = "已签收";
				} else if (obj.statu == "2") {
					obj.statuname = "已结束";
				} else {
					obj.statuname = "---";
				}*/
				$scope.scrollItems.push(obj);
			}
			if(data.object.length==0){
				$scope.hasdata = false;
			}
			
    });

	$scope.detail = function(obj) {

		$state.go('it-eventdetail', {
			'id' : obj.serialno
		});
	}

});
// 事件详情查询
appControllers.controller('It_EventDetailCtrl', function($scope, $http,$stateParams, $ionicPopup,$ionicHistory) {

	App.setScope($scope);
	App.userquery();

	var p = {
		"serialno" : $stateParams.id
	};

	var reqdata = {
		'method' : 'OrderQuery',
		'param' : JSON.stringify(p)
	};
	$scope.slbtndisabled = true;
	$scope.tjbtndisabled = true;

	$scope.showsolution = false;

	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
	 		var obj = data.object;
			if (obj.statu == "0") {
				//obj.statuname = "未签收";
				//opbtn_text = "受理";
				$scope.slbtndisabled = false;
			} else if (obj.statu == "1") {
				//obj.statuname = "已签收";
				//opbtn_text = "提交";
				$scope.tjbtndisabled = false;
				$scope.showsolution = true;
				
			} else if (obj.statu == "2") {
				//obj.statuname = "已结束";
			} else {
				//obj.statuname = "---";
			}
			obj.typeName=App.getTypeName(obj.category);
			$scope.data = obj;
			$scope.data.serialno = $stateParams.id;

    });

	// 工单受理，提交
	$scope.doprocess = function(data) {
		
		if($scope.data.statu == "0"){//受理

			var p = {
				"serialno" : data.serialno
			};

			var reqdata = {
				'method' : 'TakeOrder',
				'param' : JSON.stringify(p)
			};

			App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		 		var alertPopup = $ionicPopup.alert({
					title : '操作成功',
					template : '受理成功！'
				});
				alertPopup.then(function(res) {
					$ionicHistory.goBack();
				});
    		});

		}else if ($scope.data.statu == "1") {//提交
			
			if ($scope.data.solution == undefined || $scope.data.solution == '') {
				var alertPopup = $ionicPopup.alert({
					title : '输入错误',
					template : '请输入解决方案！'
				});
				return;
			}
			var p = {
				"serialno" : data.serialno,
				"solution":data.solution
			};
			var reqdata = {
				'method' : 'EventSubmit',
				'param' : JSON.stringify(p)
			};

			App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		 		
				var alertPopup = $ionicPopup.alert({
					title : '操作成功',
					template : '提交成功！'
				});
				alertPopup.then(function(res) {
					$ionicHistory.goBack();
				});
    		});

		}else{
			var alertPopup = $ionicPopup.alert({
				title : '提示',
				template : '不允许操作！'
			});
		};

	}
});

// 新开事件
appControllers.controller('It_EventNewCtrl', function($rootScope, $scope,$http,$ionicActionSheet, Camera) {

	App.setScope($scope);
	App.userquery();

	$scope.eventDispatch = function(isValid) {

		var paramdata = {
			title : $scope.formData.title,
			user : $scope.formData.user,
			userdeptid : $scope.formData.userdeptid,
			impact : $scope.formData.impact,
			urgency : $scope.formData.urgency,
			priority : $scope.formData.priority,
			majorevent : '',
			description : $scope.formData.description,
			files:$scope.fileids
		};
		if ($scope.formData.majorevent == true) {
			paramdata.majorevent = "Y";
		} else {
			paramdata.majorevent = "N";
		}
		var reqdata = {
			'method' : 'EventDispatch',
			'param' : JSON.stringify(paramdata)
		};

		// alert(JSON.stringify(paramdata))
		// return;
		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
			App.myNotice("新开事件成功！");
    	});
	}

	  /*Photo*/
	$scope.images_url = "";
  function onPhotoDone(imageURI){uploadPhoto(imageURI);}
  function onPhotoFail(message){Yibeiban.myLogger('Photo Failed: ' + message);}
  function makePhoto(){

  	var use_camera = Store.loadObject("use_camera");
  	if(use_camera != undefined && use_camera == "1"){
  		navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI});

  	}else{

	  	var confirmPopup = $ionicPopup.confirm({
			title : '<strong>提示</strong>',
			template : 'IT自助服务将会使用手机的摄像头，请授权。',
			okText : '允许',
			cancelText : '不允许'
		});

		confirmPopup.then(function(res) {
			if (res) {
				Store.saveObject("use_camera","1");
				navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI});
			}
		});
  	}

  }
  function takePhoto(){
    navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI
    ,sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM });
  }
  function uploadPhoto(imageURI){
  	//alert("imageURI==="+imageURI);
    var done = function(r){
    	//alert("upload done"+r.response);
      try{
      	var json = JSON.parse(r.response);
      	if(json.code=="200"){
      		var ids = json.object.ids;
      		$scope.fileids=ids;
      		$scope.images_url = imageURI;
      		App.myNotice("图片上传成功",2000);
      	}else{
      		App.myNotice("图片上传失败",2000);
      	}
      }catch(e){
      	App.myNotice("图片上传失败"+e,2000);
      }

      
      //alert($scope.images_list.length);
    };

    var fail = function(e){
    	App.myNotice("图片上传失败fail"+e,2000);
    };

    var options = new FileUploadOptions();
    options.params  = {'method' : 'FileUpload'};
    options.fileKey ="file";
    options.fileName="test.jpg";
    options.mimeType="image/jpeg";

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI(OpenAPI.requrl), done, fail, options);
  }


  $scope.showActionsheet = function(){
    $ionicActionSheet.show({
      titleText : '上传图片',
      cancelText: '取消',
      buttons   : [{text:'拍照'},{text:'从相册中选取'}],
      cancel    : function(){},
      buttonClicked : function(index){
        switch(index){
          case  1:takePhoto();break;
          case  0:
          default:makePhoto();break;
        }
        return true;
      }
    });
  };

});

// 新服务请求
appControllers.controller('It_FuwuqingqiuCtrl', function($rootScope, $scope,$http,$ionicActionSheet, Camera) {
	
	App.setScope($scope);
	App.userquery();

	$scope.eventDispatch = function(isValid) {

		var paramdata = {
			title : $scope.formData.title,
			user : $scope.formData.user,
			userdeptid : $scope.formData.userdeptid,
			impact : $scope.formData.impact,
			urgency : $scope.formData.urgency,
			priority : $scope.formData.priority,
			majorevent : '',
			description : $scope.formData.description,
			files:$scope.fileids
		};
		if ($scope.formData.majorevent == true) {
			paramdata.majorevent = "Y";
		} else {
			paramdata.majorevent = "N";
		}
		var reqdata = {
			'method' : 'EventDispatch',
			'param' : JSON.stringify(paramdata)
		};

		// alert(JSON.stringify(paramdata))
		// return;

		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
			App.myNotice("新增服务请求成功！");
    	});
	}

	 /*Photo*/
	$scope.images_url = "";
  function onPhotoDone(imageURI){uploadPhoto(imageURI);}
  function onPhotoFail(message){Yibeiban.myLogger('Photo Failed: ' + message);}
  function makePhoto(){

  	var use_camera = Store.loadObject("use_camera");
  	if(use_camera != undefined && use_camera == "1"){
  		navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI});

  	}else{

	  	var confirmPopup = $ionicPopup.confirm({
			title : '<strong>提示</strong>',
			template : 'IT自助服务将会使用手机的摄像头，请授权。',
			okText : '允许',
			cancelText : '不允许'
		});

		confirmPopup.then(function(res) {
			if (res) {
				Store.saveObject("use_camera","1");
				navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI});
			}
		});
  	}

  }
  function takePhoto(){
    navigator.camera.getPicture(onPhotoDone, onPhotoFail, { quality: 100, targetWidth: 500, targetHeight: 500, destinationType: navigator.camera.DestinationType.FILE_URI
    ,sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM });
  }
  function uploadPhoto(imageURI){
  	//alert("imageURI==="+imageURI);
    var done = function(r){
    	//alert("upload done"+r.response);
      try{
      	var json = JSON.parse(r.response);
      	if(json.code=="200"){
      		var ids = json.object.ids;
      		$scope.fileids=ids;
      		$scope.images_url = imageURI;
      		App.myNotice("图片上传成功",2000);
      	}else{
      		App.myNotice("图片上传失败",2000);
      	}
      }catch(e){
      	App.myNotice("图片上传失败"+e,2000);
      }

      
      //alert($scope.images_list.length);
    };

    var fail = function(e){
    	App.myNotice("图片上传失败fail"+e,2000);
    };

    var options = new FileUploadOptions();
    options.params  = {'method' : 'FileUpload'};
    options.fileKey ="file";
    options.fileName="test.jpg";
    options.mimeType="image/jpeg";

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI(OpenAPI.requrl), done, fail, options);
  }


  $scope.showActionsheet = function(){
    $ionicActionSheet.show({
      titleText : '上传图片',
      cancelText: '取消',
      buttons   : [{text:'拍照'},{text:'从相册中选取'}],
      cancel    : function(){},
      buttonClicked : function(index){
        switch(index){
          case  1:takePhoto();break;
          case  0:
          default:makePhoto();break;
        }
        return true;
      }
    });
  };

});
// 待办服务请求列表
appControllers.controller('It_FuwuqingqiuListCtrl', function($scope, $http,$state) {
	
	var p = {
		"type" : "DB",
		"flowcode" : "ServiceReq",
		"dbtype":'1'
	};
	var reqdata = {
		'method' : 'OrderList',
		'param' : JSON.stringify(p)
	};

	$scope.scrollItems = [];
	$scope.hasdata = true;
	
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		for (var i = 0; i < data.object.length; i++) {
				var obj = data.object[i];
				/*if (obj.statu == "0") {
					obj.statuname = "未签收";
				} else if (obj.statu == "1") {
					obj.statuname = "已签收";
				} else if (obj.statu == "2") {
					obj.statuname = "已结束";
				} else {
					obj.statuname = "---";
				}*/
				$scope.scrollItems.push(obj);
			}

			if(data.object.length==0){
				$scope.hasdata = false;
			}
    });

	$scope.detail = function(obj) {
		
		$state.go('it-fuwuqingqiuDetail', {
			"id" : obj.serialno
		});
	}
});
// 服务请求详情查询
appControllers.controller('It_FuwuqingqiuDetailCtrl', function($scope, $http,$stateParams,$ionicPopup,$ionicHistory) {

	App.setScope($scope);
	App.userquery();

	var p = {
		"serialno" : $stateParams.id
	};
	
	var reqdata = {
		'method' : 'OrderQuery',
		'param' : JSON.stringify(p)
	};

	$scope.slbtndisabled = true;
	$scope.tjbtndisabled = true;

	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		//$scope.data = data.object;
		//$scope.data.serialno = $stateParams.id;

		var obj = data.object;
			if (obj.statu == "0") {
				$scope.slbtndisabled = false;
			} else if (obj.statu == "1") {
				$scope.tjbtndisabled = false;
			} else if (obj.statu == "2") {
				
			} else {
				
			}
			obj.typeName=App.getTypeName(obj.category);
			$scope.data = obj;
			$scope.data.serialno = $stateParams.id;

    });

	// 确认解决
	$scope.doProcess = function(data) {
		//alert(data.serialno);

		var p = {
			"serialno" : data.serialno,
			"solution":""
		};

		
		var reqdata = {
			'method' : 'TakeOrder',
			'param' : JSON.stringify(p)
		};

		if($scope.data.statu == "0"){//受理
			App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
				
				//$scope.data.statu = "1";
				
				//App.myNotice("受理成功！");

				var alertPopup = $ionicPopup.alert({
					title : '操作成功',
					template : '受理成功！'
				});
				alertPopup.then(function(res) {
					$ionicHistory.goBack();
				});

		    });

		}else if ($scope.data.statu == "1") {//提交
			
			var reqdata = {
				'method' : 'ServiceReqSubmit',
				'param' : JSON.stringify(p)
			};

			App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
				
				//App.myNotice("提交成功！");
				var alertPopup = $ionicPopup.alert({
					title : '操作成功',
					template : '提交成功！'
				});
				alertPopup.then(function(res) {
					$ionicHistory.goBack();
				});
		    });

		}else{
			var alertPopup = $ionicPopup.alert({
				title : '提示',
				template : '不允许操作！'
			});
		};

	}
});

// 公告列表
appControllers.controller('It_GonggaoListCtrl',function($scope, $http) {
	var p = {
		"type" : "P"
	};
	var reqdata = {
		'method' : 'NoticeList',
		'param' : JSON.stringify(p)
	};

	$scope.hasdata = true;
	
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		
		$scope.scrollItems = data.object;
		if(data.object.length==0){
			$scope.hasdata = false;
		}
    });

});
// 运维动态列表
appControllers.controller('It_YunweiListCtrl',function($scope, $http, Loading) {
	var p = {
		"type" : "Y"
	};
	var reqdata = {
		'method' : 'NoticeList',
		'param' : JSON.stringify(p)
	};

	$scope.hasdata = true;
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		$scope.scrollItems = data.object;
		if(data.object.length==0){
			$scope.hasdata = false;
		}
    });

});
// 绩效列表
appControllers.controller('It_JixiaoListCtrl', function($scope, $http, $state) {

	var reqdata = {
		'method' : 'OfficeScore'
	};

	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		$scope.scrollItems = data.object;
    });

});

// 排班列表
appControllers.controller('It_PaibanListCtrl', function($scope, $http, $state) {
	var currentuserid = Session.loadObject("user").userid;
	var currentdate = new Date().Format('yyyy-MM-dd');
	$scope.date = currentdate;
	
	var data = [
		{"userid":"111","username":"看看1","zao1":true,"zhong1":false,"wan1":true},
		{"userid":"111","username":"看看2","zao1":true,"zhong1":false,"wan1":true},
		{"userid":"111","username":"看看3","zao1":false,"zhong1":true,"wan1":true}
	]

	//$scope.scrollItems=data;

	$scope.scrollItems = [];


    $scope.loadlist = function(date){
    	$scope.logbtndisabled = true;
    	var curpaiban = [];
    	$scope.scrollItems = [];
    	var p = {
			"date" : date
		};
		var reqdata = {
			'method' : 'PaibanList',
			'param' : JSON.stringify(p)
		};

		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		
			for (var i = 0; i < data.object.length; i++) {
				var obj = data.object[i];
				var userid = obj.userid;

				if (obj.zao == "1") {
					obj.zao1 = true;
					if(currentuserid==userid){
						curpaiban.push("1");
					}
					if (obj.zao_type == "PAIBANTYPE4"||obj.zao_type == "PAIBANTYPE5") {
						obj.zao_style="border-color: #7D7D7D;    background-color: #7D7D7D;";
					}

				}else {
					obj.zao1 = false;
				}

				if (obj.zhong == "1") {
					obj.zhong1 = true;
					if(currentuserid==userid){
						curpaiban.push("2");
					}
					if (obj.zhong_type == "PAIBANTYPE4"||obj.zhong_type == "PAIBANTYPE5") {
						obj.zhong_style="border-color: #7D7D7D;    background-color: #7D7D7D;";
					}
				}else {
					obj.zhong1 = false;
				}

				if (obj.wan == "1") {
					obj.wan1 = true;
					if(currentuserid==userid){
						curpaiban.push("3");
					}
					if (obj.wan_type == "PAIBANTYPE4"||obj.wan_type == "PAIBANTYPE5") {
						obj.wan_style="border-color: #7D7D7D;    background-color: #7D7D7D;";
					}
				}else {
					obj.wan1 = false;
				}

				$scope.scrollItems.push(obj);
			}

			if(curpaiban.length>0 && currentdate == date){
				$scope.logbtndisabled = false;
			}
			
    	});

    }

    $scope.loadlist(currentdate);

    $scope.loadnextday=function(){
    	var d = addDate($scope.date,1);
    	$scope.date = d;

    	$scope.loadlist(d);
    }

	$scope.loadpretday=function(){
    	var d = addDate($scope.date,-1);
    	$scope.date = d;

    	$scope.loadlist(d);
    }
	
	$scope.paibanLogList = function() {

		$state.go('it-paibanloglist', {"date":$scope.date});
	}

	$scope.edit = function(item,type) {

		$state.go('it-paibanedit', {"date":$scope.date,"banci":type,"userid":item.userid,"username":item.username});
	}

	/*$scope.change = function(item,type) {

		var v = '';
		if(type=="1"){
			v=item.zao1?'0':'1';
		}else if (type=="2") {
			
			v=item.zhong1?'0':'1';
		}else if (type=="3") {
			
			v=item.wan1?'0':'1';
		}

		var p = {
			"date" : $scope.date,
			"userid":item.userid,
			"type":type,
			"value":v
		};
		
		var reqdata = {
			'method' : 'ModifyPanban',
			'param' : JSON.stringify(p)
		};
		
		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){

			App.myNotice("修改成功");

			if(type=="1"){
				item.zao1=!item.zao1;
			}else if (type=="2") {
				item.zhong1=!item.zhong1;
			}else if (type=="3") {
				item.wan1=!item.wan1;
			}

			if(currentuserid==item.userid){
				if (item.zao1==false && item.zhong1==false && item.wan1==false) {
					$scope.logbtndisabled = true;
				}else{
					$scope.logbtndisabled = false;
				}
			}
		});

	}*/

});

// 排班修改
appControllers.controller('It_PaibanEditCtrl', function($scope, $http,$state,$stateParams,$ionicPopup,$ionicHistory) {

	var user = Session.loadObject("user");
	var currentdate = new Date().Format('yyyy-MM-dd');
	var date = $stateParams.date;
	var banci = $stateParams.banci;
	var userid = $stateParams.userid;
	var username = $stateParams.username;

	var curdate =  new Date(currentdate.replace(/-/g,"/"));
    var pbdate = new Date(date.replace(/-/g,"/"));

	$scope.formData={};
	$scope.date = date;
	$scope.username = username;

	$scope.cancelbtndisabled = true;
	$scope.pbbtndisabled = true;
	if (user.shizhuren && pbdate>=curdate) {
		$scope.pbbtndisabled = false;
	};

	if(banci == "1"){
		$scope.banciname="早班";
	}else if (banci == "2") {
		$scope.banciname="中班";
	}else if (banci == "3") {
		$scope.banciname="晚班";
	}

	var p = {
		"date" : date,
		"userid":userid,
		"banci":banci
	};
	
	var reqdata = {
		'method' : 'PaibanDetail',
		'param' : JSON.stringify(p)
	};

	$scope.addresslist = [];
	$scope.typelist = [];

	App.ajaxPost(OpenAPI.requrl, $.param({'method' : 'PaibanAddressList'}), function(data){
		$scope.addresslist = data.object;

		App.ajaxPost(OpenAPI.requrl, $.param({'method' : 'PaibanTypeList'}), function(data){
			$scope.typelist = data.object;
			
			App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
				var obj = data.object;
				$scope.formData.pbtype = obj.pbtype;
				$scope.formData.address = obj.address;

				if (obj.haspb == "1" && user.shizhuren  && pbdate>=curdate) {
					$scope.cancelbtndisabled = false;
				}
			});

		});

	});


	$scope.savePaiban=function(optype){

		if (optype=="1") {
			if ($scope.formData.pbtype == undefined || $scope.formData.pbtype == '') {
				var alertPopup = $ionicPopup.alert({
					title : '输入错误',
					template : '请选择排班类型！'
				});
				return;
			}
			if ($scope.formData.address == undefined || $scope.formData.address == '') {
				var alertPopup = $ionicPopup.alert({
					title : '输入错误',
					template : '请选择排班地点！'
				});
				return;
			}
		}
		
		var p = {
			"date" : date,
			"userid":userid,
			"type":banci,
			"pbtype":$scope.formData.pbtype,
			"address":$scope.formData.address,
			"value":optype
		};
		
		var reqdata = {
			'method' : 'ModifyPanban',
			'param' : JSON.stringify(p)
		};

		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
			
			var alertPopup = $ionicPopup.alert({
				title : '操作成功',
				template : (optype=="0"?'取消排班成功！':'修改排班成功！')
			});
			alertPopup.then(function(res) {
				$ionicHistory.goBack();
			});

		});
	}
});

// 排班日志列表
appControllers.controller('It_PaibanLogListCtrl', function($scope, $http,$state,$stateParams,$ionicPopup) {

	var user = Session.loadObject("user");
	var date = $stateParams.date;
	$scope.date = date;
	
	var p = {
		"date" : date
	};
	
	var reqdata = {
		'method' : 'PaibanLogList',
		'param' : JSON.stringify(p)
	};
	$scope.scrollItems = [];
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		$scope.scrollItems = data.object;
		
	});

	$scope.formData={};
	$scope.formData.banci="1"
	$scope.savePaibanLog=function(){

		if ($scope.formData.banci == undefined || $scope.formData.banci == '') {
			var alertPopup = $ionicPopup.alert({
				title : '输入错误',
				template : '请确认班次不能为空！'
			});
			return;
		}
		if ($scope.formData.content == undefined || $scope.formData.content == '') {
			var alertPopup = $ionicPopup.alert({
				title : '输入错误',
				template : '请确认内容不能为空！'
			});
			return;
		}
		var p = {
			"date" : date,
			"userid":user.userid,
			"banci":$scope.formData.banci,
			"content":$scope.formData.content
		};
		
		var reqdata = {
			'method' : 'AddPanbanLog',
			'param' : JSON.stringify(p)
		};

		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
			App.myNotice("操作成功");
		});
	}
});


//待处理工单报表
appControllers.controller('It_DclgdReportCtrl', function($rootScope,$scope, $http, $state,$ionicLoading, $ionicPopup) {

var colors = Highcharts.getOptions().colors;
	var idx = Math.floor(Math.random() * 9);

	var reqdata = {
		'method' : 'ReportDB'
	};
  	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		$scope.data =[
	   		{
	   			y: parseInt(data.object.servicereq),
                color: colors[idx]
	   		},
	   		{
	   			y: parseInt(data.object.event),
                color: colors[idx+1]
	   		}
	   ];

	$scope.chartConfig = {
	    options: {
	      chart: {
	        type: 'column'
	      },
        legend: {
            enabled: false
        },
        title: {
	      text: '待处理工单报表'
	    }
	    },

    	series: [{
            name: '数量',
            data:$scope.data,
            dataLabels: {
                enabled: true,
                rotation: -0,
                color: '#FFFFFF',
                align: 'center',
                x: 4,
                y: 20,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black'
                }
            }
        }],
        xAxis: {
            categories: [
                '服务请求',
                '故障'
            ],
            labels: {
                //rotation: -45,
                align: 'center',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: '工单数量'
            }
        }
  }
	});


});
//已处理工单报表
appControllers.controller('It_YclgdReportCtrl', function($rootScope,$scope, $http, $state,$ionicLoading, $ionicPopup) {
	var colors = Highcharts.getOptions().colors;
	var idx = Math.floor(Math.random() * 9);

	var reqdata = {
		'method' : 'ReportYB'
	};
  	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		$scope.data =[
	   		{
	   			y: parseInt(data.object.servicereq),
                color: colors[idx]
	   		},
	   		{
	   			y: parseInt(data.object.event),
                color: colors[idx+1]
	   		}
	   ];

	$scope.chartConfig = {
	    options: {
	      chart: {
	        type: 'column'
	      },
        legend: {
            enabled: false
        }
	    },

    	series: [{
            name: '数量',
            data:$scope.data,
            dataLabels: {
                enabled: true,
                rotation: -0,
                color: '#FFFFFF',
                align: 'center',
                x: 4,
                y: 20,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black'
                }
            }
        }],
        xAxis: {
            categories: [
                '服务请求',
                '故障'
            ],
            labels: {
                //rotation: -45,
                align: 'center',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: '工单数量'
            }
        },
	    title: {
	      text: '已处理工单报表'
	    }
  }
	});

	   

});
//历史工单处理情况报表
appControllers.controller('It_LsgdReportCtrl', function($rootScope,$scope, $http, $state,$ionicLoading, $ionicPopup,$ionicModal) {


	$ionicModal.fromTemplateUrl('templates/datemodal.html', 
        function(modal) {
            $scope.datemodal = modal;
        },
        {
        // Use our scope for the scope of the modal to keep it simple
        scope: $scope, 
        // The animation we want to use for the modal entrance
        animation: 'slide-in-up'
        }
    );

    $scope.opendateModal = function(flag) {
      $scope.datemodal.show();
      $scope.flag = flag;
    };
    $scope.closedateModal = function(modal) {
      $scope.datemodal.hide();

      if($scope.flag == 1){
		$scope.starttime = modal;
      }else{
		$scope.endtime = modal;
      }
      
    };

	var currentdate = new Date().Format('yyyy-MM-dd');
    $scope.starttime = addDate(currentdate,-7);
    $scope.endtime = currentdate;

    $scope.doquery = function(){

    	if ($scope.starttime == undefined || $scope.starttime == '') {
			var alertPopup = $ionicPopup.alert({
				title : '输入错误',
				template : '请输入开始日期！'
			});
			return;
		}
		if ($scope.endtime == undefined || $scope.endtime == '') {
			var alertPopup = $ionicPopup.alert({
				title : '输入错误',
				template : '请输入截止日期！'
			});
			return;
		}

    	var p = {
			"starttime" : $scope.starttime,
			"endtime":$scope.endtime
		};
		
		var reqdata = {
			'method' : 'ReportQuery',
			'param' : JSON.stringify(p)
		};

		App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
			$scope.scrollItems = data.object;

			var dates = [];

			var fwqqdata = [];
			var gzdata = [];

			for(var i=0;i<data.object.length;i++){
				dates.push(data.object[i].gendate);

				fwqqdata.push(parseInt(data.object[i].servicereq));
				gzdata.push(parseInt(data.object[i].event));

			}


			$scope.chartConfig = {
			    options: {
			     legend: {
				     
				     align: 'right',
				     verticalAlign: 'top',
				     x: 5,
				     y: 50,
				     borderWidth: 0
				}
			    },

		    	series: [
		    		{
			            name: '服务请求',
			            data: fwqqdata
			        }, {
			            name: '故障',
			            data: gzdata
			        }
		    	],
		        xAxis: {
		            categories: dates
		        },
		        yAxis: {
		            min: 0,
		            tickInterval:1,
		            title: {
		                text: '工单数量'
		            }
		        },
			    title: {
			      text: '历史工单处理情况报表'
			    }
		  }
		});

    }
$scope.doquery();

});

// 意见列表
appControllers.controller('It_NotelistCtrl', function($scope, $http,$state,$ionicPopover) {

	$scope.moredata = true;
  $scope.currentPage = 1;
  $scope.pageSize = 10;
  $scope.items = [];
  var run=false;

  $scope.hasdata = true;

  $scope.cur_reply_noteid = "";
    $scope.cur_reply_index = 0;

    $scope.items1111111=[
    {
      "id": "111",
      "userid": "",
      "username": "订单",
      "content": "啊撒旦范德萨分",
      "createtime": "11111",
      "replylist": [
			{
			     "id": "",
			      "userid": "",
			      "username": "dddd",
			      "content": "adsfadsf",
			     "createtime": ""
			 },
			 {
			     "id": "",
			      "userid": "",
			      "username": "dddd",
			      "content": "ddddd点点滴滴",
			     "createtime": ""
			 }
		]
    }
  ];


  var load_datalist = function(callback){

    if(run){
      return;
    }else{
      run = true;
    }

    var p = {
		"userid":"",
		"page":$scope.currentPage+"",
		"pagesize":$scope.pageSize+""
	};
	var reqdata = {
		'method' : 'NoteList',
		'param' : JSON.stringify(p)
	};
	
	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		run = false;
        $scope.currentPage = $scope.currentPage + 1;

        var temp = [];
        for(i=0;i<data.object.length;i++){
        	var obj = data.object[i];
        	if(obj.replylist == undefined){
        		obj.replylist = [];
        	}
        	temp.push(obj);
        }
        if(data.object.length==0){
        	$scope.hasdata = false;
        }
        
        $scope.items = $scope.items.concat(temp);

        if (data.object.length < $scope.pageSize) {
          $scope.moredata = false;
        }else{
          $scope.moredata = true;
        }
        
        callback && callback();
	});

  }

  $scope.loadMore = function() {
    console.info("loadmore========")
    //alert("loadMore");
    load_datalist(function(){
      $scope.$broadcast('scroll.infiniteScrollComplete');
    });
  }
  //$scope.loadMore();

  $scope.moreDataCanBeLoaded = function() {
    return $scope.moredata;
  }

  $ionicPopover.fromTemplateUrl('templates/commont-form.html', {
    scope : $scope
  }).then(function(popover) {
    $scope.popover = popover;
  });

  $scope.openPopover = function($event,item,index) {

    $scope.cur_reply_noteid = item.noteid;
    $scope.cur_reply_index = index;

    $scope.popover.show($event);
  };
  $scope.closePopover = function() {
    $scope.popover.hide();
  };

  $scope.$on('$destroy', function() {
    $scope.popover.remove();
  });

  $scope.doreply=function(){
    
    var message = $("#replymessage").val();
    if(message == ""){
      App.myNotice("请输入内容。");
      return;
    }
    
    var user = Session.loadObject("user");


    var p = {
		"userid":user.userid,
		"username":user.name,
		"content":message,
		"noteid":$scope.cur_reply_noteid
	};
	var reqdata = {
		'method' : 'NoteReply',
		'param' : JSON.stringify(p)
	};


	App.ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
		var aa = $scope.items[$scope.cur_reply_index];
        
        aa.replylist.push({
          "username":user.name,
          "content":message
        })

        $("#replymessage").val('');
         $scope.closePopover();

        App.myNotice("回复成功。");
	});


  }


});